﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Infrastructure
{
    public static class TaskTimeoutAfter
    {
        //public static async Task TimeoutAfter(this Task task, int millisecondsDelay)
        //{
        //    var timeoutCancellationTokenSource = new CancellationTokenSource();
        //    var completedTask = await Task.WhenAny(task, Task.Delay(millisecondsDelay, timeoutCancellationTokenSource.Token));
        //    if (completedTask == task)
        //    {
        //        timeoutCancellationTokenSource.Cancel();
        //    }
        //    else
        //    {
        //        throw new TimeoutException("The operation has timed out.");
        //    }
        //}

        public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, int millisecondsDelay)
        {
            var timeoutCancellationTokenSource = new CancellationTokenSource();
            var completedTask = await Task.WhenAny(task, Task.Delay(millisecondsDelay, timeoutCancellationTokenSource.Token));
            if (completedTask == task)
            {
                timeoutCancellationTokenSource.Cancel();
                return task.Result;
            }
            else
            {
                throw new TimeoutException("The operation has timed out.");
            }
        }
    }
}
